﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Skynet.ServiceContracts;
using Microsoft.Practices.Unity;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.Unity.Configuration;
using System.Security.Principal;
using System.ServiceModel;
using System.ServiceModel.Activation; 
namespace Skynet.ServiceImplementation
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]    
    public class SkynetSecurityTest : ISkynetSecurityTest
    {
        [Dependency]
        public IUnityContainer Container { get; set; }

        [Dependency]
        public BusinessLogic.BusinessComponents.IBCExams BCExams { get; set; }

        [OperationBehavior(Impersonation=ImpersonationOption.Required)]
        public string GetStatus(string input)
        {
            StringBuilder sb = new StringBuilder();  
            
            if (OperationContext.Current.ServiceSecurityContext != null)
            {
                sb.AppendLine(string.Format("Authentication type {0}", OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.AuthenticationType));
                sb.AppendLine(string.Format(".Net user {0}", OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.Name));
                sb.AppendLine(string.Format(".Net Is authenticate {0}", OperationContext.Current.ServiceSecurityContext.PrimaryIdentity.IsAuthenticated));
                sb.AppendLine(string.Format("Windows Is authenticate {0}", OperationContext.Current.ServiceSecurityContext.WindowsIdentity.IsAuthenticated));
            }
            else
            {
                sb.AppendLine("UnAuthenticate");
            }
            sb.AppendLine("Windows User : " + WindowsIdentity.GetCurrent().Name);
            return sb.ToString();
            //var defaultWriter = Container.Resolve<LogWriter>();
            //if (defaultWriter.IsLoggingEnabled())
            //{
            //    defaultWriter.Write(string.Format("Windows Identi ty {0}", WindowsIdentity.GetCurrent().Name), Infrastructure.Aspect.Constants.LoggingPolicyNames.AspectServicePolicy);
            //}
        }
    }
}
